<?php
/* SVN FILE: $Id: process.php 255 2009-12-22 06:45:47Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Bank - Process bank account action
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 255 $
* @lastrevision $Date: 2009-12-22 06:45:47 +0000 (Tue, 22 Dec 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-12-22 06:45:47 +0000 (Tue, 22 Dec 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/plugins/bank/publicAction/process.php $
*/

class plugin_public_bank_process extends marketPublicCommand
{
	/**
	 * Main class entry point
	 *
	 * @access	public
	 * @param	object		ipsRegistry reference
	 * @return	void		[Outputs to screen]
	 */
	public function doExecute( ipsRegistry $registry )
	{
		if(!marketRegistry::hasPluginPermission('use'))
		{
			$this->registry->getClass('output')->showError('no_permission');
		}
		
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'bank_bank' ) );
		
		$this->request['account_id'] = intval($this->request['account_id']);
		$account = $this->DB->buildAndFetch(array('select'=>'*','from'=>'market_bank_accounts','where'=>"account_id=".$this->request['account_id']));
		
		if(!$account || $account['a_mem_id'] != $this->memberData['member_id'])
		{
			$this->registry->getClass('output')->showError('no_permission');
		}
		
		$this->request['amount'] = numbers::parseInt($this->request['amount']);
		
		$account['accountID'] = bankPluginFunctions::getBankAccountID($account);
		if($this->request['do'] == 'withdraw')
		{
			$account['a_withdraw_fee'] = numbers::multiply($account['a_withdraw_fee'],ipsRegistry::member()->getProperty('market_deduct'));
			$fee = numbers::round(numbers::divide(numbers::multiply($this->request['amount'],$account['a_withdraw_fee']),100));
			
			$balance = numbers::subtract($account['balance'],$this->request['amount']);
			$money = numbers::subtract(numbers::add($this->memberData[$this->settings['market_members_field']],$this->request['amount']),$fee);
			
			if(numbers::compare($this->request['amount']) < 0 || numbers::compare($balance) < 0 || numbers::compare($money) < 0)
			{
				$this->registry->getClass('output')->showError('not_enough_money');
			}
			
			$this->DB->update('market_bank_accounts',array('balance'=>$balance),"account_id=".$this->request['account_id']);
			$this->DB->update('members',array(
												$this->settings['market_members_field'] => $money,
			),"member_id=".$this->memberData['member_id']);
			
			bankPluginFunctions::logTransaction($account['accountID'],'withdraw',$this->request['amount'],$balance,$fee);
		}
		elseif($this->request['do'] == 'deposit')
		{
			$account['a_deposit_fee'] = numbers::multiply($account['a_deposit_fee'],ipsRegistry::member()->getProperty('market_deduct'));
			$fee = numbers::round(numbers::divide(numbers::multiply($this->request['amount'],$account['a_deposit_fee']),100));
			
			$balance = numbers::add($account['balance'],$this->request['amount']);
			$money = numbers::subtract($this->memberData[$this->settings['market_members_field']],numbers::add($this->request['amount'],$fee));
			
			if(numbers::compare($this->request['amount']) < 0 || numbers::compare($money) < 0)
			{
				$this->registry->getClass('output')->showError('not_enough_money');
			}
			
			$this->DB->update('market_bank_accounts',array('balance'=>$balance),"account_id=".$this->request['account_id']);
			$this->DB->update('members',array(
												$this->settings['market_members_field'] => $money,
			),"member_id=".$this->memberData['member_id']);
			
			bankPluginFunctions::logTransaction($account['accountID'],'deposit',$this->request['amount'],$balance,$fee);
		}
		elseif($this->request['do'] == 'remove')
		{
			$account['a_delete_fee'] = numbers::multiply($account['a_delete_fee'],ipsRegistry::member()->getProperty('market_deduct'));
			$money = numbers::subtract($this->memberData[$this->settings['market_members_field']],$account['a_delete_fee']);
			
			if(numbers::compare($money) < 0)
			{
				$this->registry->getClass('output')->showError('not_enough_money');
			}
			if(numbers::compare($account['balance']) != 0)
			{
				$this->registry->getClass('output')->showError('account_not_empty');
			}
			
			$this->DB->delete('market_bank_accounts',"account_id=".$this->request['account_id']);
			$this->DB->update('members',array(
												$this->settings['market_members_field'] => $money,
			),"member_id=".$this->memberData['member_id']);
			
			bankPluginFunctions::logTransaction($account['accountID'],'remove',0,0,$account['a_delete_fee']);
		}
		
		$this->registry->getClass('output')->redirectScreen( $this->lang->words['processed_request'], $this->settings['base_url_with_app'] . "plugin=".$this->request['plugin'] . "&amp;action=manage&amp;account_id=" . $this->request['account_id'] );
	}
}